/**
* Class Name: iFulfillCurrentActCntrlr 
* Author: Accenture
* Date: 13-Jun-2011
* Requirement/Project Name: iFulfill
* Requirement/Project Description: Activity Checklist VF controller
**/

public with sharing class iFulfillCurrentActCntrlr {
    Public list<actWrapper> actWrap{get;set;}    
    Fullfilment_Item__c fItem;
    list<FulFillment_Activities__c> currentActs = new list<FulFillment_Activities__c>();    
    public iFulfillCurrentActCntrlr(ApexPages.StandardController controller) {
        fItem = (Fullfilment_Item__c)controller.getRecord();
    }
    
    public void listACtivities(){
        actWrap = new list<actWrapper>();
       if(fItem.Current_Step__C != null) // display all activities for current task
            currentActs = [select id, name,Completed_By__c,Completed_Date__c, activity_name__c, required__c, status__c from FulFillment_Activities__c where Fullfilment_Item__c = :fItem.id and Wokflow_Steps__c = :fItem.Current_Step__C];
        for(FulFillment_Activities__c FA : currentActs ){
            actWrap.add(new actWrapper(false,FA));
        }
    }
    
    //wrapper to display and bind records with selectbox
    Class actWrapper{
       public boolean selBox{get;set;}
       public FulFillment_Activities__c FA{get;set;}
       public actWrapper(boolean sel,FulFillment_Activities__c fulfillAct ){
           selBox = sel;
           FA = fulfillAct ;
       }
    }
    
    //Marks selected activities as complete
    Public Pagereference doComplete(){
        list<FulFillment_Activities__c> updatedFA = new list<FulFillment_Activities__c>();
        for(actWrapper aWrap :actWrap){
            if(aWrap.selBox &&  aWrap.FA.status__c != IfullfillTextConstant.ACTIVITY_COMPLETE){
                aWrap.FA.status__c = IfullfillTextConstant.ACTIVITY_COMPLETE;
                aWrap.FA.Completed_Date__c =system.today();
                aWrap.FA.Completed_By__c = userinfo.getuserid();
                updatedFA.add(aWrap.FA); 
            } 
         }
         try{
             update updatedFA;
             listACtivities();
         }     
         Catch(dmlException e){
            IfullfillTextConstant.parseExceptionMsg(e,'iFulfillCurrentActCntrlr','doComplete');
            listACtivities();            
         }
        return null;        
     }
        
   //Updates selected activities as not complete
    Public Pagereference doUnComplete(){
        list<FulFillment_Activities__c> updatedFA = new list<FulFillment_Activities__c>();
        for(actWrapper aWrap :actWrap){
            if(aWrap.selBox &&  aWrap.FA.status__c == IfullfillTextConstant.ACTIVITY_COMPLETE){
                aWrap.FA.status__c = IfullfillTextConstant.ACTIVITY_NOT_COMPLETE;
                aWrap.FA.Completed_Date__c =null;
                aWrap.FA.Completed_By__c = null;
                updatedFA.add(aWrap.FA); 
            } 
        }
        try{
            update updatedFA;
            listACtivities();
        }     
         Catch(dmlException e){
            IfullfillTextConstant.parseExceptionMsg(e,'iFulfillCurrentActCntrlr','doUnComplete');
            listACtivities();
         }
        return null;
      } 
    
      //disable buttons if FI owner is queue
    public boolean getDisableButton()
    { 
         Profile currentUserProfile  = [select name from profile  where id  = :userinfo.getProfileId()];
         String ownerID = fItem.OwnerId;
         Boolean index =ownerID.startsWith('005'); 
      
         if (fItem .Status__c  !=IfullfillTextConstant.FI_WORKFLOW_LIVE_STATUS && fItem .Status__c  !=IfullfillTextConstant.FI_WORKFLOW_OUTED_STATUS && fItem .Status__c  !=IfullfillTextConstant.FI_WORKFLOW_CANCELED_STATUS && index  && !currentUserProfile.name.startsWith(IfullfillTextConstant.SFA_PROFILE_NAME_STARTS_WITH ))
            return false;
         else
            return true;
    }
}